package com.tywj.lib.social.pay

import android.app.Activity
import com.tywj.lib.social.entity.PayOrderEntity
import com.tywj.lib.social.SocialUtil
import com.tywj.lib.social.interfaces.IPay
import com.tywj.lib.social.interfaces.Callback

import com.tencent.mm.opensdk.modelpay.PayReq
import com.tywj.lib.core.common.util.ToastUtil
import com.blankj.utilcode.util.AppUtils
import com.tywj.lib.social.R

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/16
 *     desc   : 微信支付
 *     version: 1.0.0
 * </pre>
 */
class WeChatPay :
    IPay<PayOrderEntity> {

    override fun pay(entity: PayOrderEntity, activity: Activity, callback: Callback) {
        // 判断用户是否安装微信客户端
        if (!SocialUtil.wxApi.isWXAppInstalled) {
            ToastUtil.shortToast(R.string.social_not_install_wechat)
            return
        }

        // 判断用户安装的微信是否支持支付
        if (SocialUtil.wxApi.wxAppSupportAPI <= AppUtils.getAppVersionCode()) {
            ToastUtil.shortToast(R.string.social_not_support_wechat_pay)
            return
        }

        SocialUtil.pay.callback = callback

        val request = PayReq()
        request.appId = entity.appid
        request.partnerId = entity.partnerid
        request.prepayId = entity.prepayid
        request.packageValue = entity.thepackage
        request.nonceStr = entity.noncestr
        request.timeStamp = entity.timestamp
        request.sign = entity.sign
        SocialUtil.wxApi.sendReq(request)
    }
}